/*
 * @Author: your name
 * @Date: 2021-11-05 11:13:40
 * @LastEditTime: 2022-09-15 16:13:26
 * @LastEditors: shijian.chen shijian.chen@qunar.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /ten-pms-app-web/src/components/form/libs/index.ts
 */
import ownerValidator from "./validator";
import { Rules } from '../typings.d'
export const checkValue = async (rules: Rules[] | undefined, value: string | boolean) => {
    let message: any = "";
    if(rules){
        for (let i = 0; i < rules.length; i++) {
            let item: Rules = rules[i];
            if (item.required) {
                if ((value + "").trim() == "") {
                    message = item.message;
                } else {
                }
            } else {
                if (item.type) {
                    if (item.type != "callback") {
                        message = ownerValidator[item.type] ? ownerValidator[item.type]({value:value, message:item.message}) : '';
                    }else{
                        message = item.callback ? await item.callback(value) : '';
                    }
                }
            }
            if (message) {
                return message;
            }
        }
        return message
    }
    return message;
};

// export function checkErrorByRules(val,rules){
//     let obj = {
//         val:val
//     }
//     let error = "";
//     if (rules) {
//         for (var index = 0; index < rules.length; index++) {
//             var item = rules[index];
//             if (validator[item.checkType] instanceof Function) {
//                 error = validator[item.checkType].call(obj, item);
//                 if (error) {
//                     break;
//                 }
//             }
//         }
//     }
//     return error;
// }
